package com.yuba.front.controller.post;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yuba.common.core.domain.R;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.common.satoken.utils.LoginHelper;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.bo.PostsBo;
import com.yuba.front.domain.bo.PostsPushBo;
import com.yuba.front.domain.param.DelParam;
import com.yuba.front.domain.param.QueryParam;
import com.yuba.front.domain.vo.PostsHotVo;
import com.yuba.front.domain.vo.PostsVo;
import com.yuba.front.service.IPostsService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 前台帖子接口
 *
 * @author modakai
 * @date 2024-12-02
 */
@RestController
@RequestMapping("api/posts/")
@RequiredArgsConstructor
public class ApiPostsController extends BaseController {

    private final IPostsService postsService;

    /**
     * 分页获取列表
     * 推荐列表：type: recommend
     * 热门列表：type: hot
     * 精选列表：choice
     *
     * @return 列表信息
     */
    @SaIgnore
    @PostMapping("page/list")
    public R<Map<String, Object>> list(@RequestBody QueryParam queryParam) {
        return R.ok(postsService.apiQueryPageList(queryParam));
    }

    /**
     * 获取热门文章列表
     *      文章从开始到现在的热门文章列表
     * @return 列表信息
     */
    @SaIgnore
    @GetMapping("list/hot")
    public R<List<PostsHotVo>> hot(@RequestParam(defaultValue = "0") Long categoryId) {
        return R.ok(postsService.apiQueryHotList(categoryId));
    }

    /**
     * 获取文章详情
     * @param postsId 文章id
     * @return 文章详情
     */
    @SaIgnore
    @GetMapping("{postsId}")
    public R<PostsVo> getDetail(@PathVariable("postsId") Long postsId) {
        return R.ok(postsService.apiDetail(postsId));
    }

    /**
     * 更新帖子阅读次数
     * @return 无
     */
    @SaIgnore
    @PostMapping("posts_view")
    public R<Void> postsView(@RequestBody PostsBo postsBo) {
        postsService.postsView(postsBo.getPostId());
        return R.ok();
    }


    /**
     * 查询当前登录用户上次未发布的文章
     */
    @GetMapping("get/draft")
    public R<PostsVo> getDraft() {
        return R.ok(postsService.getDraft());
    }

    /**
     * 获取归属于用户的帖子
     */
    @SaIgnore
    @GetMapping("list/user")
    public R<TableDataInfo<PostsVo>> getUserPosts(PageQuery pageQuery, Long categoryId) {
        PostsBo postsBo = new PostsBo();
        postsBo.setCategoryId(categoryId);
        postsBo.setUserId(LoginHelper.getUserId());
        return R.ok(postsService.queryPageList(postsBo, pageQuery));
    }

    /**
     * 获取收藏的帖子
     */
    @GetMapping("user/collect")
    public R<TableDataInfo<PostsVo>> getCollectPosts(PageQuery pageQuery) {
        return R.ok(postsService.queryCollectPageList(pageQuery, LoginHelper.getUserId()));
    }


    /**
     * 发布帖子
     * @param postsPush 帖子信息
     * @return 无
     */
    @PostMapping("push")
    public R<Void> save(@RequestBody PostsPushBo postsPush) {
        postsService.push(postsPush);
        return R.ok();
    }

    /**
     * 删除帖子
     * @param delForm 删除参数
     * @return 无
     */
    @PostMapping("delete")
    public R<Void> delete(@Validated @RequestBody DelParam delForm) {
        postsService.delete(delForm, LoginHelper.getUserId());
        return R.ok();
    }


}
